Dynomotion

Group: DynoMotion Message: 13343 From: Colin Fera Date: 6/11/2016
Subject: thread dies when running Gcode
Hi, 

I have the code below flashed to thread 2. It starts up fine and works as intended initially but dies when running GCode using kmotion CNC. 

Does anyone see any issues with the code or with this approach.

Thanks,



code:  

#include "KMotionDef.h"
#include "PC-DSP.h"
#define TMP 10 
#include "KflopToKMotionCNCFunctions.c"

double DROx, DROy, DROz, DROa, DROb, DROc;

void SetSoftLim(void);
void home(void);
void estop(void);
void SetZ(void);

void RST_ENC(void);
void Disable(void);
void Enable(void);
int enable=0;
int SL=-1;
#define ESTOP_BIT 139
#define ZLIM_BIT 139
#define XLIM_BIT 138
#define YLIM_BIT 137

#define XDrive  152
#define YDrive  153
#define ZDrive  154
#define Lube   155      
#define spindle  0
#define spindle_CLK  144
#define spindle_COCLK  146
#define ESTOP_BIT 139
#define ZLIM_BIT 139
#define XLIM_BIT 138
#define YLIM_BIT 137
#define X 0
#define Y 1
#define Z 2
#define MaxY 130000
#define MinY -1000
#define MaxZ 0
#define MinZ -80000
#define MaxX 196000
#define MinX -1000

void main(void){
Disable();
printf("Thread 2 init\n");
while(1){
estop();
SetSoftLim();
home();
RST_ENC();
WaitNextTimeSlice();
}


}
 
void estop(void){
if(ch0->Enable || ch1->Enable || ch2->Enable){
if (!ReadBit(ESTOP_BIT) || !ReadBit(ZLIM_BIT) || !ReadBit(YLIM_BIT) || !ReadBit(XLIM_BIT)  )
{
Disable();
printf("Spindle off drives disabled E Stop\n");
}
else if(ReadBit(55)) {
Disable();
printf("Spindle off drives disabled you pushed axis disable button\n");
}
}
else if(ReadBit(55) && ReadBit(ESTOP_BIT) && ReadBit(ZLIM_BIT) && ReadBit(YLIM_BIT) && ReadBit(XLIM_BIT) ){
Enable();
printf("Spindle and drives enabled you pushed axis enable button\n");
}
}
void SetSoftLim(void){
if(ReadBit(48) && SL) {
ClearStopImmediately();
//disable soft limits
ch0->SoftLimitPos= 800000;       
ch0->SoftLimitNeg=-800000;         
ch1->SoftLimitPos=800000;         
ch1->SoftLimitNeg=-800000;       
ch2->SoftLimitPos=800000;       
ch2->SoftLimitNeg=-800000;
ClearBit(48);
SL=0;  
printf("soft limits disabled \n");
}
    else if (ReadBit(48) && !SL || SL==-1)
{
//enable soft limits
ch0->SoftLimitPos=MaxX;       
ch0->SoftLimitNeg=MinX;         
ch1->SoftLimitPos=MaxY;         
ch1->SoftLimitNeg=MinY;       
ch2->SoftLimitPos=MaxZ;       
ch2->SoftLimitNeg=MinZ;
ClearBit(48);
SL=1;          
printf("soft limits enabled \n");
}
return;
}
void SetZ(void){
if(ReadBit(53)) {
ch2->SoftLimitNeg=ch2->Dest;      
ClearBit(53);
}
if(ReadBit(54)) {
ch2->SoftLimitNeg=MinZ;  
        ClearBit(54);
}
return;
}
void home (void){
if(ReadBit(49)){  //home sequence X,Y
   ClearStopImmediately();
Zero(X);
Zero(Y);
ClearBit(49);
}
if(ReadBit(52)){  //home sequence Z
   ClearStopImmediately();
Zero(Z);
ClearBit(52);
}
return;
}

}

void RST_ENC(void){
if(ReadBit(56)){
EnableAxisDest(X,ch0->Position);
EnableAxisDest(Y,ch1->Position);
EnableAxisDest(Z,ch2->Position);
ClearBit(56);
}
return;
}

void Enable(void){
enable=1;
   EnableAxisDest(Z,ch2->Dest);  //Z
       EnableAxisDest(Y,ch1->Dest);   //Y
   EnableAxisDest(X,ch0->Dest);   //X
SetBit(XDrive);
SetBit(154);
SetBit(153);
SetBit(155);
ClearBit(55);
ClearBit(145);
ClearBit(144);
ClearBit(146);
printf("Spindle and drives Enabled\n");
DAC(0,0);
ch0->SoftLimitPos=MaxX;       
ch0->SoftLimitNeg=MinX;         
ch1->SoftLimitPos=MaxY;         
ch1->SoftLimitNeg=MinY;       
ch2->SoftLimitPos=MaxZ;       
ch2->SoftLimitNeg=MinZ;
SL=1;          
printf("soft limits enabled \n");
ClearBit(48); 
}
void Disable(void){
DisableAxis(Z);  // axis still enabled?  - Disable it
DisableAxis(Y);  // axis still enabled?  - Disable it
DisableAxis(X);  // axis still enabled?  - Disable it
ClearBit(55);
ClearBit(XDrive);
ClearBit(154);
ClearBit(153);
ClearBit(155);
SetBit(145);

ClearBit(146);
ClearBit(144);
DAC(0,0);
enable=0;

}

Group: DynoMotion Message: 13345 From: Tom Kerekes Date: 6/12/2016
Subject: Re: thread dies when running Gcode
Hi Scott,

Do you have anything in KMotionCNC configured to use Thread #2?  If so it would kill and overwrite that program when downloaded and executed. 

Regards
TK

On Jun 11, 2016, at 9:32 PM, Colin Fera colin.fera@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:

 

Hi, 

I have the code below flashed to thread 2. It starts up fine and works as intended initially but dies when running GCode using kmotion CNC. 

Does anyone see any issues with the code or with this approach.

Thanks,



code:  

#include "KMotionDef.h"
#include "PC-DSP.h"
#define TMP 10 
#include "KflopToKMotionCNCFunctions.c"

double DROx, DROy, DROz, DROa, DROb, DROc;

void SetSoftLim(void);
void home(void);
void estop(void);
void SetZ(void);

void RST_ENC(void);
void Disable(void);
void Enable(void);
int enable=0;
int SL=-1;
#define ESTOP_BIT 139
#define ZLIM_BIT 139
#define XLIM_BIT 138
#define YLIM_BIT 137

#define XDrive  152
#define YDrive  153
#define ZDrive  154
#define Lube   155      
#define spindle  0
#define spindle_CLK  144
#define spindle_COCLK  146
#define ESTOP_BIT 139
#define ZLIM_BIT 139
#define XLIM_BIT 138
#define YLIM_BIT 137
#define X 0
#define Y 1
#define Z 2
#define MaxY 130000
#define MinY -1000
#define MaxZ 0
#define MinZ -80000
#define MaxX 196000
#define MinX -1000

void main(void){
Disable();
printf("Thread 2 init\n");
while(1){
estop();
SetSoftLim();
home();
RST_ENC();
WaitNextTimeSlice();
}


}
 
void estop(void){
if(ch0->Enable || ch1->Enable || ch2->Enable){
if (!ReadBit(ESTOP_BIT) || !ReadBit(ZLIM_BIT) || !ReadBit(YLIM_BIT) || !ReadBit(XLIM_BIT)  )
{
Disable();
printf("Spindle off drives disabled E Stop\n");
}
else if(ReadBit(55)) {
Disable();
printf("Spindle off drives disabled you pushed axis disable button\n");
}
}
else if(ReadBit(55) && ReadBit(ESTOP_BIT) && ReadBit(ZLIM_BIT) && ReadBit(YLIM_BIT) && ReadBit(XLIM_BIT) ){
Enable();
printf("Spindle and drives enabled you pushed axis enable button\n");
}
}
void SetSoftLim(void){
if(ReadBit(48) && SL) {
ClearStopImmediately();
//disable soft limits
ch0->SoftLimitPos= 800000;       
ch0->SoftLimitNeg=-800000;         
ch1->SoftLimitPos=800000;         
ch1->SoftLimitNeg=-800000;       
ch2->SoftLimitPos=800000;       
ch2->SoftLimitNeg=-800000;
ClearBit(48);
SL=0;  
printf("soft limits disabled \n");
}
    else if (ReadBit(48) && !SL || SL==-1)
{
//enable soft limits
ch0->SoftLimitPos=MaxX;       
ch0->SoftLimitNeg=MinX;         
ch1->SoftLimitPos=MaxY;         
ch1->SoftLimitNeg=MinY;       
ch2->SoftLimitPos=MaxZ;       
ch2->SoftLimitNeg=MinZ;
ClearBit(48);
SL=1;          
printf("soft limits enabled \n");
}
return;
}
void SetZ(void){
if(ReadBit(53)) {
ch2->SoftLimitNeg=ch2->Dest;      
ClearBit(53);
}
if(ReadBit(54)) {
ch2->SoftLimitNeg=MinZ;  
        ClearBit(54);
}
return;
}
void home (void){
if(ReadBit(49)){  //home sequence X,Y
   ClearStopImmediately();
Zero(X);
Zero(Y);
ClearBit(49);
}
if(ReadBit(52)){  //home sequence Z
   ClearStopImmediately();
Zero(Z);
ClearBit(52);
}
return;
}

}

void RST_ENC(void){
if(ReadBit(56)){
EnableAxisDest(X,ch0->Position);
EnableAxisDest(Y,ch1->Position);
EnableAxisDest(Z,ch2->Position);
ClearBit(56);
}
return;
}

void Enable(void){
enable=1;
   EnableAxisDest(Z,ch2->Dest);  //Z
       EnableAxisDest(Y,ch1->Dest);   //Y
   EnableAxisDest(X,ch0->Dest);   //X
SetBit(XDrive);
SetBit(154);
SetBit(153);
SetBit(155);
ClearBit(55);
ClearBit(145);
ClearBit(144);
ClearBit(146);
printf("Spindle and drives Enabled\n");
DAC(0,0);
ch0->SoftLimitPos=MaxX;       
ch0->SoftLimitNeg=MinX;         
ch1->SoftLimitPos=MaxY;         
ch1->SoftLimitNeg=MinY;       
ch2->SoftLimitPos=MaxZ;       
ch2->SoftLimitNeg=MinZ;
SL=1;          
printf("soft limits enabled \n");
ClearBit(48); 
}
void Disable(void){
DisableAxis(Z);  // axis still enabled?  - Disable it
DisableAxis(Y);  // axis still enabled?  - Disable it
DisableAxis(X);  // axis still enabled?  - Disable it
ClearBit(55);
ClearBit(XDrive);
ClearBit(154);
ClearBit(153);
ClearBit(155);
SetBit(145);

ClearBit(146);
ClearBit(144);
DAC(0,0);
enable=0;

}

Group: DynoMotion Message: 13347 From: Colin Fera Date: 6/12/2016
Subject: Re: thread dies when running Gcode
Hi Tom,

Verified just in-case, nothing else is running in thread 2.

Thanks,
Colin

On Sun, Jun 12, 2016 at 6:23 AM, Tom Kerekes tk@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Hi Scott,

Do you have anything in KMotionCNC configured to use Thread #2?  If so it would kill and overwrite that program when downloaded and executed. 

Regards
TK

On Jun 11, 2016, at 9:32 PM, Colin Fera colin.fera@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:

 

Hi, 

I have the code below flashed to thread 2. It starts up fine and works as intended initially but dies when running GCode using kmotion CNC. 

Does anyone see any issues with the code or with this approach.

Thanks,



code:  

#include "KMotionDef.h"
#include "PC-DSP.h"
#define TMP 10 
#include "KflopToKMotionCNCFunctions.c"

double DROx, DROy, DROz, DROa, DROb, DROc;

void SetSoftLim(void);
void home(void);
void estop(void);
void SetZ(void);

void RST_ENC(void);
void Disable(void);
void Enable(void);
int enable=0;
int SL=-1;
#define ESTOP_BIT 139
#define ZLIM_BIT 139
#define XLIM_BIT 138
#define YLIM_BIT 137

#define XDrive  152
#define YDrive  153
#define ZDrive  154
#define Lube   155      
#define spindle  0
#define spindle_CLK  144
#define spindle_COCLK  146
#define ESTOP_BIT 139
#define ZLIM_BIT 139
#define XLIM_BIT 138
#define YLIM_BIT 137
#define X 0
#define Y 1
#define Z 2
#define MaxY 130000
#define MinY -1000
#define MaxZ 0
#define MinZ -80000
#define MaxX 196000
#define MinX -1000

void main(void){
Disable();
printf("Thread 2 init\n");
while(1){
estop();
SetSoftLim();
home();
RST_ENC();
WaitNextTimeSlice();
}


}
 
void estop(void){
if(ch0->Enable || ch1->Enable || ch2->Enable){
if (!ReadBit(ESTOP_BIT) || !ReadBit(ZLIM_BIT) || !ReadBit(YLIM_BIT) || !ReadBit(XLIM_BIT)  )
{
Disable();
printf("Spindle off drives disabled E Stop\n");
}
else if(ReadBit(55)) {
Disable();
printf("Spindle off drives disabled you pushed axis disable button\n");
}
}
else if(ReadBit(55) && ReadBit(ESTOP_BIT) && ReadBit(ZLIM_BIT) && ReadBit(YLIM_BIT) && ReadBit(XLIM_BIT) ){
Enable();
printf("Spindle and drives enabled you pushed axis enable button\n");
}
}
void SetSoftLim(void){
if(ReadBit(48) && SL) {
ClearStopImmediately();
//disable soft limits
ch0->SoftLimitPos= 800000;       
ch0->SoftLimitNeg=-800000;         
ch1->SoftLimitPos=800000;         
ch1->SoftLimitNeg=-800000;       
ch2->SoftLimitPos=800000;       
ch2->SoftLimitNeg=-800000;
ClearBit(48);
SL=0;  
printf("soft limits disabled \n");
}
    else if (ReadBit(48) && !SL || SL==-1)
{
//enable soft limits
ch0->SoftLimitPos=MaxX;       
ch0->SoftLimitNeg=MinX;         
ch1->SoftLimitPos=MaxY;         
ch1->SoftLimitNeg=MinY;       
ch2->SoftLimitPos=MaxZ;       
ch2->SoftLimitNeg=MinZ;
ClearBit(48);
SL=1;          
printf("soft limits enabled \n");
}
return;
}
void SetZ(void){
if(ReadBit(53)) {
ch2->SoftLimitNeg=ch2->Dest;      
ClearBit(53);
}
if(ReadBit(54)) {
ch2->SoftLimitNeg=MinZ;  
        ClearBit(54);
}
return;
}
void home (void){
if(ReadBit(49)){  //home sequence X,Y
   ClearStopImmediately();
Zero(X);
Zero(Y);
ClearBit(49);
}
if(ReadBit(52)){  //home sequence Z
   ClearStopImmediately();
Zero(Z);
ClearBit(52);
}
return;
}

}

void RST_ENC(void){
if(ReadBit(56)){
EnableAxisDest(X,ch0->Position);
EnableAxisDest(Y,ch1->Position);
EnableAxisDest(Z,ch2->Position);
ClearBit(56);
}
return;
}

void Enable(void){
enable=1;
   EnableAxisDest(Z,ch2->Dest);  //Z
       EnableAxisDest(Y,ch1->Dest);   //Y
   EnableAxisDest(X,ch0->Dest);   //X
SetBit(XDrive);
SetBit(154);
SetBit(153);
SetBit(155);
ClearBit(55);
ClearBit(145);
ClearBit(144);
ClearBit(146);
printf("Spindle and drives Enabled\n");
DAC(0,0);
ch0->SoftLimitPos=MaxX;       
ch0->SoftLimitNeg=MinX;         
ch1->SoftLimitPos=MaxY;         
ch1->SoftLimitNeg=MinY;       
ch2->SoftLimitPos=MaxZ;       
ch2->SoftLimitNeg=MinZ;
SL=1;          
printf("soft limits enabled \n");
ClearBit(48); 
}
void Disable(void){
DisableAxis(Z);  // axis still enabled?  - Disable it
DisableAxis(Y);  // axis still enabled?  - Disable it
DisableAxis(X);  // axis still enabled?  - Disable it
ClearBit(55);
ClearBit(XDrive);
ClearBit(154);
ClearBit(153);
ClearBit(155);
SetBit(145);

ClearBit(146);
ClearBit(144);
DAC(0,0);
enable=0;

}


Group: DynoMotion Message: 13348 From: Tom Kerekes Date: 6/12/2016
Subject: Re: thread dies when running Gcode

Hi Colin,

Is there a Green Bar over Thread #2 in KMotion.exe C Programs Screen to show it is running?

Maybe Single Step through the GCode and try to determine what statement kills it.

BTW in KMotionCNC the Emergency Stop Button (Stop Sign) will Kill all threads except Thread #1.  We normally recommend not Flashing programs to KFLOP and instead using a loop to your Init C Program for that type of functionality.

Regards

TK


On 6/12/2016 9:24 AM, Colin Fera colin.fera@... [DynoMotion] wrote:
 
Hi Tom,

Verified just in-case, nothing else is running in thread 2.

Thanks,
Colin

On Sun, Jun 12, 2016 at 6:23 AM, Tom Kerekes tk@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 
Hi Scott,

Do you have anything in KMotionCNC configured to use Thread #2?  If so it would kill and overwrite that program when downloaded and executed. 

Regards
TK

On Jun 11, 2016, at 9:32 PM, Colin Fera colin.fera@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:

 
Hi, 

I have the code below flashed to thread 2. It starts up fine and works as intended initially but dies when running GCode using kmotion CNC. 

Does anyone see any issues with the code or with this approach.

Thanks,



code:  

#include "KMotionDef.h"
#include "PC-DSP.h"
#define TMP 10 
#include "KflopToKMotionCNCFunctions.c"

double DROx, DROy, DROz, DROa, DROb, DROc;

void SetSoftLim(void);
void home(void);
void estop(void);
void SetZ(void);

void RST_ENC(void);
void Disable(void);
void Enable(void);
int enable=0;
int SL=-1;
#define ESTOP_BIT 139
#define ZLIM_BIT 139
#define XLIM_BIT 138
#define YLIM_BIT 137

#define XDrive  152
#define YDrive  153
#define ZDrive  154
#define Lube   155      
#define spindle  0
#define spindle_CLK  144
#define spindle_COCLK  146
#define ESTOP_BIT 139
#define ZLIM_BIT 139
#define XLIM_BIT 138
#define YLIM_BIT 137
#define X 0
#define Y 1
#define Z 2
#define MaxY 130000
#define MinY -1000
#define MaxZ 0
#define MinZ -80000
#define MaxX 196000
#define MinX -1000

void main(void){
Disable();
printf("Thread 2 init\n");
while(1){
estop();
SetSoftLim();
home();
RST_ENC();
WaitNextTimeSlice();
}


}
 
void estop(void){
if(ch0->Enable || ch1->Enable || ch2->Enable){
if (!ReadBit(ESTOP_BIT) || !ReadBit(ZLIM_BIT) || !ReadBit(YLIM_BIT) || !ReadBit(XLIM_BIT)  )
{
Disable();
printf("Spindle off drives disabled E Stop\n");
}
else if(ReadBit(55)) {
Disable();
printf("Spindle off drives disabled you pushed axis disable button\n");
}
}
else if(ReadBit(55) && ReadBit(ESTOP_BIT) && ReadBit(ZLIM_BIT) && ReadBit(YLIM_BIT) && ReadBit(XLIM_BIT) ){
Enable();
printf("Spindle and drives enabled you pushed axis enable button\n");
}
}
void SetSoftLim(void){
if(ReadBit(48) && SL) {
ClearStopImmediately();
//disable soft limits
ch0->SoftLimitPos= 800000;       
ch0->SoftLimitNeg=-800000;         
ch1->SoftLimitPos=800000;         
ch1->SoftLimitNeg=-800000;       
ch2->SoftLimitPos=800000;       
ch2->SoftLimitNeg=-800000;
ClearBit(48);
SL=0;  
printf("soft limits disabled \n");
}
    else if (ReadBit(48) && !SL || SL==-1)
{
//enable soft limits
ch0->SoftLimitPos=MaxX;       
ch0->SoftLimitNeg=MinX;         
ch1->SoftLimitPos=MaxY;         
ch1->SoftLimitNeg=MinY;       
ch2->SoftLimitPos=MaxZ;       
ch2->SoftLimitNeg=MinZ;
ClearBit(48);
SL=1;          
printf("soft limits enabled \n");
}
return;
}
void SetZ(void){
if(ReadBit(53)) {
ch2->SoftLimitNeg=ch2->Dest;      
ClearBit(53);
}
if(ReadBit(54)) {
ch2->SoftLimitNeg=MinZ;  
        ClearBit(54);
}
return;
}
void home (void){
if(ReadBit(49)){  //home sequence X,Y
   ClearStopImmediately();
Zero(X);
Zero(Y);
ClearBit(49);
}
if(ReadBit(52)){  //home sequence Z
   ClearStopImmediately();
Zero(Z);
ClearBit(52);
}
return;
}

}

void RST_ENC(void){
if(ReadBit(56)){
EnableAxisDest(X,ch0->Position);
EnableAxisDest(Y,ch1->Position);
EnableAxisDest(Z,ch2->Position);
ClearBit(56);
}
return;
}

void Enable(void){
enable=1;
   EnableAxisDest(Z,ch2->Dest);  //Z
       EnableAxisDest(Y,ch1->Dest);   //Y
   EnableAxisDest(X,ch0->Dest);   //X
SetBit(XDrive);
SetBit(154);
SetBit(153);
SetBit(155);
ClearBit(55);
ClearBit(145);
ClearBit(144);
ClearBit(146);
printf("Spindle and drives Enabled\n");
DAC(0,0);
ch0->SoftLimitPos=MaxX;       
ch0->SoftLimitNeg=MinX;         
ch1->SoftLimitPos=MaxY;         
ch1->SoftLimitNeg=MinY;       
ch2->SoftLimitPos=MaxZ;       
ch2->SoftLimitNeg=MinZ;
SL=1;          
printf("soft limits enabled \n");
ClearBit(48); 
}
void Disable(void){
DisableAxis(Z);  // axis still enabled?  - Disable it
DisableAxis(Y);  // axis still enabled?  - Disable it
DisableAxis(X);  // axis still enabled?  - Disable it
ClearBit(55);
ClearBit(XDrive);
ClearBit(154);
ClearBit(153);
ClearBit(155);
SetBit(145);

ClearBit(146);
ClearBit(144);
DAC(0,0);
enable=0;

}



Group: DynoMotion Message: 13349 From: Colin Fera Date: 6/12/2016
Subject: Re: thread dies when running Gcode
Hi Tom,

The green bar is there.  So I guess the thread is getting stuck somewhere in the code.

I will probably put this back in the init thread.  I had it there originally and never notice any issues.

Thanks,
Colin

On Sun, Jun 12, 2016 at 9:57 AM, Tom Kerekes tk@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Hi Colin,

Is there a Green Bar over Thread #2 in KMotion.exe C Programs Screen to show it is running?

Maybe Single Step through the GCode and try to determine what statement kills it.

BTW in KMotionCNC the Emergency Stop Button (Stop Sign) will Kill all threads except Thread #1.  We normally recommend not Flashing programs to KFLOP and instead using a loop to your Init C Program for that type of functionality.

Regards

TK


On 6/12/2016 9:24 AM, Colin Fera colin.fera@... [DynoMotion] wrote:
 
Hi Tom,

Verified just in-case, nothing else is running in thread 2.

Thanks,
Colin

On Sun, Jun 12, 2016 at 6:23 AM, Tom Kerekes tk@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 
Hi Scott,

Do you have anything in KMotionCNC configured to use Thread #2?  If so it would kill and overwrite that program when downloaded and executed. 

Regards
TK

On Jun 11, 2016, at 9:32 PM, Colin Fera colin.fera@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:

 
Hi, 

I have the code below flashed to thread 2. It starts up fine and works as intended initially but dies when running GCode using kmotion CNC. 

Does anyone see any issues with the code or with this approach.

Thanks,



code:  

#include "KMotionDef.h"
#include "PC-DSP.h"
#define TMP 10 
#include "KflopToKMotionCNCFunctions.c"

double DROx, DROy, DROz, DROa, DROb, DROc;

void SetSoftLim(void);
void home(void);
void estop(void);
void SetZ(void);

void RST_ENC(void);
void Disable(void);
void Enable(void);
int enable=0;
int SL=-1;
#define ESTOP_BIT 139
#define ZLIM_BIT 139
#define XLIM_BIT 138
#define YLIM_BIT 137

#define XDrive  152
#define YDrive  153
#define ZDrive  154
#define Lube   155      
#define spindle  0
#define spindle_CLK  144
#define spindle_COCLK  146
#define ESTOP_BIT 139
#define ZLIM_BIT 139
#define XLIM_BIT 138
#define YLIM_BIT 137
#define X 0
#define Y 1
#define Z 2
#define MaxY 130000
#define MinY -1000
#define MaxZ 0
#define MinZ -80000
#define MaxX 196000
#define MinX -1000

void main(void){
Disable();
printf("Thread 2 init\n");
while(1){
estop();
SetSoftLim();
home();
RST_ENC();
WaitNextTimeSlice();
}


}
 
void estop(void){
if(ch0->Enable || ch1->Enable || ch2->Enable){
if (!ReadBit(ESTOP_BIT) || !ReadBit(ZLIM_BIT) || !ReadBit(YLIM_BIT) || !ReadBit(XLIM_BIT)  )
{
Disable();
printf("Spindle off drives disabled E Stop\n");
}
else if(ReadBit(55)) {
Disable();
printf("Spindle off drives disabled you pushed axis disable button\n");
}
}
else if(ReadBit(55) && ReadBit(ESTOP_BIT) && ReadBit(ZLIM_BIT) && ReadBit(YLIM_BIT) && ReadBit(XLIM_BIT) ){
Enable();
printf("Spindle and drives enabled you pushed axis enable button\n");
}
}
void SetSoftLim(void){
if(ReadBit(48) && SL) {
ClearStopImmediately();
//disable soft limits
ch0->SoftLimitPos= 800000;       
ch0->SoftLimitNeg=-800000;         
ch1->SoftLimitPos=800000;         
ch1->SoftLimitNeg=-800000;       
ch2->SoftLimitPos=800000;       
ch2->SoftLimitNeg=-800000;
ClearBit(48);
SL=0;  
printf("soft limits disabled \n");
}
    else if (ReadBit(48) && !SL || SL==-1)
{
//enable soft limits
ch0->SoftLimitPos=MaxX;       
ch0->SoftLimitNeg=MinX;         
ch1->SoftLimitPos=MaxY;         
ch1->SoftLimitNeg=MinY;       
ch2->SoftLimitPos=MaxZ;       
ch2->SoftLimitNeg=MinZ;
ClearBit(48);
SL=1;          
printf("soft limits enabled \n");
}
return;
}
void SetZ(void){
if(ReadBit(53)) {
ch2->SoftLimitNeg=ch2->Dest;      
ClearBit(53);
}
if(ReadBit(54)) {
ch2->SoftLimitNeg=MinZ;  
        ClearBit(54);
}
return;
}
void home (void){
if(ReadBit(49)){  //home sequence X,Y
   ClearStopImmediately();
Zero(X);
Zero(Y);
ClearBit(49);
}
if(ReadBit(52)){  //home sequence Z
   ClearStopImmediately();
Zero(Z);
ClearBit(52);
}
return;
}

}

void RST_ENC(void){
if(ReadBit(56)){
EnableAxisDest(X,ch0->Position);
EnableAxisDest(Y,ch1->Position);
EnableAxisDest(Z,ch2->Position);
ClearBit(56);
}
return;
}

void Enable(void){
enable=1;
   EnableAxisDest(Z,ch2->Dest);  //Z
       EnableAxisDest(Y,ch1->Dest);   //Y
   EnableAxisDest(X,ch0->Dest);   //X
SetBit(XDrive);
SetBit(154);
SetBit(153);
SetBit(155);
ClearBit(55);
ClearBit(145);
ClearBit(144);
ClearBit(146);
printf("Spindle and drives Enabled\n");
DAC(0,0);
ch0->SoftLimitPos=MaxX;       
ch0->SoftLimitNeg=MinX;         
ch1->SoftLimitPos=MaxY;         
ch1->SoftLimitNeg=MinY;       
ch2->SoftLimitPos=MaxZ;       
ch2->SoftLimitNeg=MinZ;
SL=1;          
printf("soft limits enabled \n");
ClearBit(48); 
}
void Disable(void){
DisableAxis(Z);  // axis still enabled?  - Disable it
DisableAxis(Y);  // axis still enabled?  - Disable it
DisableAxis(X);  // axis still enabled?  - Disable it
ClearBit(55);
ClearBit(XDrive);
ClearBit(154);
ClearBit(153);
ClearBit(155);
SetBit(145);

ClearBit(146);
ClearBit(144);
DAC(0,0);
enable=0;

}